/**
 * 
 */
package com.exject.android.geobasedsoundmanager;

import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;

import com.exject.android.geobasedsoundmanager.settings.Settings;
import com.exject.android.geobasedsoundmanager.settings.SettingsImpl;
import com.google.android.maps.GeoPoint;

/**
 * @author Atracura
 * 
 */
public class SettingsActivity extends PreferenceActivity implements
		OnPreferenceChangeListener {
	private static final int E6_MULTIPLY = 1000000;
	
	private static final String START_ADDRESS = "address";
	private static final String START_LATITUDE = "latitude";
	private static final String START_LONGITUDE = "longitude";
	private static final String DEFAULT_ADDRESS = "defaultAddress";
	private static final String START_POSITION_PER_MAP = "startPositionPerMap";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);

		Preference pref = this.findPreference(START_ADDRESS);
		pref.setOnPreferenceChangeListener(this);
		
		pref = this.findPreference(START_LATITUDE);
		pref.setOnPreferenceChangeListener(this);
		
		pref = this.findPreference(START_LONGITUDE);
		pref.setOnPreferenceChangeListener(this);
		
		pref = this.findPreference(DEFAULT_ADDRESS);
		pref.setOnPreferenceChangeListener(this);
		
		pref = this.findPreference(START_POSITION_PER_MAP);
		pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			
			public boolean onPreferenceClick(Preference arg0) {
				Toast.makeText(SettingsActivity.this, "Casd" , 2000).show();
				return false;
			}
		});
	}

	public boolean onPreferenceChange(Preference preference, Object newValue) {
		Settings settings = null;
		if(SettingsImpl.singletonAvaible()) {
			settings = SettingsImpl.getInstance();
		} else {
			settings = SettingsImpl.initInstance(this);
		}
		if (preference.getKey().equals(START_ADDRESS)) {
			String address = (String)newValue;
			settings.setStartAddress(address);
			return true;
		}
		if(preference.getKey().equals(START_LATITUDE)) {
			int latitude = Integer.getInteger((String)newValue)*E6_MULTIPLY;
			GeoPoint point = settings.getStartPosition();
			GeoPoint newPoint = new GeoPoint(latitude, point.getLongitudeE6());
			settings.setStartPosition(newPoint);
			return true; 
		}
		if(preference.getKey().equals(START_LONGITUDE)) {
			//TODO: NullPointerException
			int longitude = Integer.getInteger((String)newValue)*E6_MULTIPLY;
			GeoPoint point = settings.getStartPosition();
			GeoPoint newPoint = new GeoPoint(point.getLatitudeE6(), longitude);
			settings.setStartPosition(newPoint);
			return true;
		}
		if(preference.getKey().equals(DEFAULT_ADDRESS)) {
			boolean defaultAddress = ((Boolean)newValue).booleanValue();
			settings.setUseStartAddress(defaultAddress);
			return true;
		}
		return false;
	}
}
